<?php
/**
 * @file : celebrity.php
 * @date : 2014-12-26
 * @author : yuebin
 */
namespace haibao\www\data;
use haibao\www\model\filter\Celebrity as CelebrityFilter;
use haibao\www\model\filter\CelebrityInfo as CelebrityInfoFilter;
class Celebrity extends \haibao\www\data\BaseMysql{


    public function __construct(){
    
        parent::__construct('\haibao\www\model\data\Celebrity');
        //$this->setConfig('\haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_CMS');
    }

    /**
     * 添加明星
     * @param Object \haibao\cms\model\data\Celebrity $celebrityModel
     */
    public function addCelebrity($celebrityModel,$userName){

        $this->beginTranscation();
        try{
        
            $this->add($celebrityModel);
        	
        	$this->addBrandRelStar($celebrityModel);
        	
        	$this->addCelebrityRel($celebrityModel);
        	
        	$celebrityImageData = new CelebrityImage();
        	// 通过图片修改图片状态
        	$celebrityImageData->updateAllImageStatus($celebrityModel,\haibao\cms\model\data\ImageLibrary::STATUS_ENABLE,$userName);
            // 生成操作LOG
            $this->addEditLog(\haibao\cms\model\data\CelebrityEditLog::TYPE_ADD, $celebrityModel);
        
        } catch(\Exception $e){
        
        	$this->rollback();
        	throw $e;
        
        }
        $this->endTranscation();
        // 老表添加明星
        if(\haibao\classlibrary\www\Config::getConfig(\haibao\classlibrary\www\Config::SYNC_OLD)){
            $this->addLibraryCelebrity($celebrityModel,$userName);
    	}
    	return $celebrityModel;
    }
    
    /**
     * 其他模块调用添加明星
     */
    public function addCelebrityForOther($celebrityModel,$userName){
    	
    	
    	$this->beginTranscation();
    	try{
    		$this->add($celebrityModel);
    		// 生成操作LOG
    		$this->addEditLog(\haibao\cms\model\data\CelebrityEditLog::TYPE_ADD, $celebrityModel);
    	
    	} catch(\Exception $e){
    	
    		$this->rollback();
    		throw $e;
    	}
    	$this->endTranscation();
    	// 老表添加明星
    	if(\haibao\classlibrary\www\Config::getConfig(\haibao\classlibrary\www\Config::SYNC_OLD)){
    		$this->addLibraryCelebrity($celebrityModel,$userName);
    	}
    	return $celebrityModel;
    }
    /**
     * 修改明星
     * @param Object \haibao\cms\model\data\Celebrity $celebrityModel
     */
    public function updateCelebrityById($celebrityModel,$userName){

        list($celebrityArrSet,$celebrityInfoArrSet) = $this->packageCelebrityFilter($celebrityModel); 

        $celebrityInfoData = new CelebrityInfo();
        $celebrityImageData = new CelebrityImage();
        
        $celebrityInfoFilter = new CelebrityInfoFilter();
        $celebrityInfoFilter->where($celebrityInfoFilter::CONDITION_FIELD_NAME_CELEBRITY_ID,\haibao\frame\data\query\Condition::CONDITION_EQUAL, $celebrityModel->Id);
        
        /*
        $celebrityImageFilter = new CelebrityImageFilter();
        $celebrityImageFilter->where($celebrityImageFilter::CONDITION_FIELD_NAME_CELEBRITY_ID,\haibao\frame\data\query\Condition::CONDITION_EQUAL, $celebrityModel->Id);
		*/
        $this->begintranscation();
        try{

            $this->updateById($celebrityModel->Id,$celebrityArrSet);
            $celebrityInfoData->update($celebrityInfoArrSet,$celebrityInfoFilter);

            $celebrityImageData->updateImageByCelebrityId($celebrityModel,$userName);
                         
            // 生成操作LOG
            $this->addEditLog(\haibao\cms\model\data\CelebrityEditLog::TYPE_UPDATE, $celebrityModel);

            // 删除上一次的明星数据 然后再添加此次数据 这样做太复杂
            // 后期考虑从数据库中获取上次数据与这次数据做比较 多出来的添加
            $this->delBrandRelStar($celebrityModel->Id);
            $this->addBrandRelStar($celebrityModel);

            $this->delCelebrityRel($celebrityModel);
            $this->addCelebrityRel($celebrityModel);

        } catch (\Exception $e){
            $this->rollback();
        }
        $this->endtranscation();
        // 老表修改明星
        if(\haibao\classlibrary\www\Config::getConfig(\haibao\classlibrary\www\Config::SYNC_OLD)){
           $this->updateLibraryCelebrity($celebrityModel);
    	}
    }

 
    /**
     * 通过中文名获取明星信息
     * @param Object \haibao\cms\model\data\Celebrity $celebrityModel
     */
    public function checkCelebrityIsExist($celebrityModel){

        $sql = 'select NameCn from `Celebrity` where NameCn=%s and NameEn=%s limit 1';
        $result = $this->query($sql, array($celebrityModel->NameCn,$celebrityModel->NameEn));
        
        while ($result->fetch_assoc()){
            return true;
        }
        return false;
    }
 
    
    /**
     * 获取明星信息
     * @param Object \haibao\cms\model\filter\Celebrity $celebrityFilter
     */
    public function getAll($celebrityFilter){
        return parent::getAll($celebrityFilter);
    }

    /**
     * 根据明星ID获取明星信息
     * @param Int $id 明星ID 
     */
    public function getCelebrityById($id){

        // 后期考虑存入缓存
        return $this->getOneById($id);
    }

    /**
     * 根据标签ID更新明星信息
     * @param : Array $arrSet
     * @param : Object \haibao\cms\model\filter\Celebrity $filter
     * @param : Object \haibao\cms\model\data\Celebrity $model
     */
    public function updateCelebrityByFilter($arrSet,$filter,$model = null){
    
    	$this->beginTranscation();
    	try{
    		$this->update($arrSet,$filter);
    
    	}catch (\Exception $e) {
    		$this->rollback();
    		throw $e;
    	}
    	$this->endTranscation();
    
    	if(\haibao\classlibrary\www\Config::getConfig(\haibao\classlibrary\www\Config::SYNC_OLD)){
    	}
    }
    /**
     * 删除明星通过tagId
     */
    public function deleteCelebrityByTagId($tagId,$filter){
    	parent::update(array(CelebrityFilter::CONDITION_FIELD_NAME_STATUS=>\haibao\cms\model\data\Celebrity::STATUS_DISABLE), $filter);
    	if(\haibao\classlibrary\www\Config::getConfig(\haibao\classlibrary\www\Config::SYNC_OLD)){
    		$this->setConfig(\haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_ARTICLE);
    		$sql ="update `library_celebrity` set status =0 where tag_id= %s";
    		$this->query($sql,array($tagId));
    	}
    }
    /**
     * 封装修改明星参数
     * @param Object \haibao\cms\model\data\Celebrity $celebrityModel
     */
    private function packageCelebrityFilter($celebrityModel){


        $celebrityArr = array(
            CelebrityFilter::CONDITION_FIELD_NAME_NAME_CN => $celebrityModel->NameCn, 
            CelebrityFilter::CONDITION_FIELD_NAME_NAME_EN => $celebrityModel->NameEn, 
            CelebrityFilter::CONDITION_FIELD_NAME_GENDER => $celebrityModel->Gender, 
            CelebrityFilter::CONDITION_FIELD_NAME_PLACE => $celebrityModel->Place, 
            CelebrityFilter::CONDITION_FIELD_NAME_STAR_TYPES => $celebrityModel->StarTypes, 
            CelebrityFilter::CONDITION_FIELD_NAME_BIRTHDAY => $celebrityModel->Birthday, 
            CelebrityFilter::CONDITION_FIELD_NAME_PINYIN_CH => $celebrityModel->PinyinCh, 
            CelebrityFilter::CONDITION_FIELD_NAME_PINYIN_EN => $celebrityModel->PinyinEn, 
            CelebrityFilter::CONDITION_FIELD_NAME_EDITOR => $celebrityModel->Editor, 
            CelebrityFilter::CONDITION_FIELD_NAME_STATUS => $celebrityModel->Status, 
            CelebrityFilter::CONDITION_FIELD_NAME_DESCRIPTION => $celebrityModel->Description, 
            CelebrityFilter::CONDITION_FIELD_NAME_UPDATE_TIME => date('Y-m-d H:i:s'), 
        );

        $celebrityInfoArr = array(
        
            CelebrityInfoFilter::CONDITION_FIELD_NAME_MORE_CH_NAME => $celebrityModel->CelebrityInfo->MoreChName,
            CelebrityInfoFilter::CONDITION_FIELD_NAME_MORE_EH_NAME => $celebrityModel->CelebrityInfo->MoreEnName,
            CelebrityInfoFilter::CONDITION_FIELD_NAME_DIE_DATE => $celebrityModel->CelebrityInfo->DieDate,
            CelebrityInfoFilter::CONDITION_FIELD_NAME_XING_ZUO => $celebrityModel->CelebrityInfo->XingZuo,
            CelebrityInfoFilter::CONDITION_FIELD_NAME_BIRTH_PLACE => $celebrityModel->CelebrityInfo->BirthPlace,
            CelebrityInfoFilter::CONDITION_FIELD_NAME_HEIGHT => $celebrityModel->CelebrityInfo->Height,
            CelebrityInfoFilter::CONDITION_FIELD_NAME_BLOOD => $celebrityModel->CelebrityInfo->Blood,
            CelebrityInfoFilter::CONDITION_FIELD_NAME_SCHOOL => $celebrityModel->CelebrityInfo->School,
            CelebrityInfoFilter::CONDITION_FIELD_NAME_EC_COMPANY => $celebrityModel->CelebrityInfo->EcCompany,
            CelebrityInfoFilter::CONDITION_FIELD_NAME_REP_WORKS => $celebrityModel->CelebrityInfo->RepWorks,
            CelebrityInfoFilter::CONDITION_FIELD_NAME_SUCCESS => $celebrityModel->CelebrityInfo->Success,
            CelebrityInfoFilter::CONDITION_FIELD_NAME_FAMILY => $celebrityModel->CelebrityInfo->Family,
        );
        /*
        $celebrityImageArr = array();
        foreach($celebrityModel->CelebrityImage as $image){
        	
        	$celebrityImageArr[CelebrityImageFilter::CONDITION_FIELD_NAME_IMAGE_ID] = $image->ImageId;
        	
        }
        $celebrityImageArr = array(
        	CelebrityImageFilter::CONDITION_FIELD_NAME_IMAGE_ID => $celebrityModel->CelebrityImage->ImageId
        );
        */
        return array($celebrityArr, $celebrityInfoArr);
    }

    /**
     * 添加明星品牌关联信息
     * @param Object \haibao\cms\model\data\Celebrity $celebrityModel
     */
    private function addBrandRelStar($celebrityModel){

        if(!empty($celebrityModel->CelebrityRelBrand)){

            $sql = 'insert into `BrandRelStar` (CelebrityId,BrandId,CooperateTime) values ';

            $sqlArr = $valArr = array();
            foreach($celebrityModel->CelebrityRelBrand as $key=>$arr){

                array_push($sqlArr,'(%s,%s,%s)');
                array_push($valArr,$celebrityModel->Id,$arr['id'],$arr['time']);
            }
            $sql .= implode(',',$sqlArr);
            $this->query($sql,$valArr);
        }
    }
    
    /**
     * 添加明星与明星关联信息
     * @param Object \haibao\cms\model\data\Celebrity $celebrityModel
     */
    private function addCelebrityRel($celebrityModel){
    	
	
    	
        if(!empty($celebrityModel->CelebrityRelStar)){

            $topicId = \haibao\cms\business\Tag::getTopicIdByTagId(array($celebrityModel->TagId));
            foreach($celebrityModel->CelebrityRelStar as $key=>$arr){
            	
            	
            	$sql = 'insert into `CelebrityRelation` (MastarCelebrity,JoinCelebrity,RelationTypeId) values (%s,%s,%s)';
            	$sqlArr = $valArr = array();
                array_push($valArr,$topicId[$celebrityModel->TagId],$arr['id'],$arr['type']);
                $this->query($sql,$valArr);
               	$celebrityModel->CelebrityRelStar[$key]['newId'] = $this->getDb()->insert_id."\n";
            }
            //$sql .= implode(',',$sqlArr);
            //$this->query($sql,$valArr);
        }
    }
    
    /**
     * 删除明星品牌关联信息
     * @param : Int $celebrityId 明星ID
     */
    private function delBrandRelStar($celebrityId){
    
    	$sql = 'delete from `BrandRelStar` where CelebrityId = %s';
    	$this->query($sql,array($celebrityId));
    }
    
    /**
     * 删除明星关联明星信息
     * @param Object \haibao\cms\model\data\Celebrity $celebrityModel
     */
    private function delCelebrityRel($celebrityModel){

    	$topicId = \haibao\cms\business\Tag::getTopicIdByTagId(array($celebrityModel->TagId));

    	$sql = 'delete from `CelebrityRelation` where MastarCelebrity = %s';
    	$this->query($sql,array($topicId[$celebrityModel->TagId]));
    }
    
    /**
     * 添加明星操作LOG
     * @param Int $type 操作类型
     * @param Object \haibao\cms\model\data\Celebrity $celebrityModel
     */
    private function addEditLog($type, $celebrityModel){

        $model = new \haibao\cms\model\data\CelebrityEditLog();
        $model->CelebrityId = $celebrityModel->Id;
        $model->UserId = $celebrityModel->Editor;
        $model->Type = $type;
        $model->EditTime = date('Y-m-d H:i:s');
        $this->add($model);
    }
    
    /**
     * 根据标签ID获取明星信息
     * @param : Array or Int $tagId
     */
    public function getCelebrityByTagId($tagId){
    	
    	$celebrityFilter = new CelebrityFilter();
    	$celebrityFilter->select(array(
    		
    		CelebrityFilter::CONDITION_FIELD_NAME_TAG_ID,
    		CelebrityFilter::CONDITION_FIELD_NAME_NAME_CN,
    		CelebrityFilter::CONDITION_FIELD_NAME_NAME_EN,
    	));

        if(is_array($tagId)){
    		$celebrityFilter->where(CelebrityFilter::CONDITION_FIELD_NAME_TAG_ID,\haibao\frame\data\query\Condition::CONDITION_IN,$tagId);

    	}else{
    		$celebrityFilter->where(CelebrityFilter::CONDITION_FIELD_NAME_TAG_ID,\haibao\frame\data\query\Condition::CONDITION_EQUAL,$tagId);
    	}
    	return $this->getAll($celebrityFilter);
    }
    
    /**
     * 根据明星ID删除明星
     */
    public function deleteCelebrityById($celebrityId, $status = \haibao\cms\model\data\Celebrity::STATUS_DISABLE){
    	 
    	try {
    		$this->updateById($celebrityId,array('Status'=>$status));
    		
    	// 老表添加明星
        if(\haibao\classlibrary\www\Config::getConfig(\haibao\classlibrary\www\Config::SYNC_OLD)){
        	
        	$data = $this->getOneById($celebrityId);
    		$this->setConfig('haibao_article');
    		if($status == 0){
    			$oldStatus = 2;
    		}elseif($status == 2){
    			$oldStatus = 0;
    		}else{
    			$oldStatus=1;
    		}
        	$sql = 'update `library_celebrity` set status=%s where tag_id=%s';
        	$this->query($sql,array($oldStatus,$data->TagId));
    	}
    	}catch (\Exception $e){
    		throw $e;
    	}
    }
    

    /**
     * 添加旧库明星
     * @param Object \haibao\cms\model\data\Celebrity $celebrityModel
     */
    private function addLibraryCelebrity($model,$userName){
    	$field = '`tag_id`,`desc`,`name_ch`,`name_en`,`gender`,`place`,`startypes`,`birthday`,`pinyin_ch`,`pinyin_en`,`status`,`date`,`tag_type`,`more_ch`,`more_en`,`die_date`,`xingzuo`,`birth_place`,`height`,`blood`,`school`,`ec_company`,`rp_works`,`success`,`family`,`editor`';
    
    	$arrSql = $arrValues = array();
    	//array_push($arrSql, '(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)');
    	array_push($arrValues, $model->TagId);
    	array_push($arrValues, $model->Description);
    	array_push($arrValues, $model->NameCn);
    	array_push($arrValues, $model->NameEn);
    	array_push($arrValues, $model->Gender);
    	array_push($arrValues, $model->Place);
    	array_push($arrValues, $model->StarTypes);
    	array_push($arrValues, $model->Birthday);
    	array_push($arrValues, $model->PinyinCh);
    	array_push($arrValues, $model->PinyinEn);
    	if($model->Status == 0){
    		$status = 2;
    	}else{
    		$status = 1;
    	}
    	array_push($arrValues, $status);
    	array_push($arrValues, date('Y-m-d H:i:s'));
    	array_push($arrValues, $model->TagType);

    	array_push($arrValues, $model->CelebrityInfo->MoreChName);
    	array_push($arrValues, $model->CelebrityInfo->MoreEnName);
    	array_push($arrValues, $model->CelebrityInfo->DieDate);
    	array_push($arrValues, $model->CelebrityInfo->XingZuo);
    	array_push($arrValues, $model->CelebrityInfo->BirthPlace);
    	array_push($arrValues, $model->CelebrityInfo->Height);
    	array_push($arrValues, $model->CelebrityInfo->Blood);
    	array_push($arrValues, $model->CelebrityInfo->School);
    	array_push($arrValues, $model->CelebrityInfo->EcCompany);
    	array_push($arrValues, strip_tags($model->CelebrityInfo->RepWorks));
    	array_push($arrValues, strip_tags($model->CelebrityInfo->Success));
    	array_push($arrValues, strip_tags($model->CelebrityInfo->Family));
    	array_push($arrValues, $userName);
    
    	if($model->CelebrityImage){
    		$field .= ',`avatar`,`avatar_640`,`avatar_710`';
	    	// 处理图片
	    	$imageIds = array();
	    	foreach($model->CelebrityImage as $image){
	    		$imageIds[] = $image->ImageId;
	    	}
	    	$images = \haibao\cms\business\ImageLibrary::getImagesById($imageIds);
	    	$imgArr = array();
	    	foreach($images as $img){
	    		$imgArr[$img->Id] = $img->Filename;
	    	}
	    	array_push($arrValues, ltrim($imgArr[$model->CelebrityImage[\haibao\cms\model\data\CelebrityImage::IMAGE_TYPE_PICTURE]->ImageId],'/'));
	    	array_push($arrValues, ltrim($imgArr[$model->CelebrityImage[\haibao\cms\model\data\CelebrityImage::IMAGE_TYPE_SCREEN]->ImageId],'/'));
	    	if(array_key_exists(\haibao\cms\model\data\CelebrityImage::IMAGE_TYPE_OTHER, $model->CelebrityImage)){
	    		array_push($arrValues, ltrim($imgArr[$model->CelebrityImage[\haibao\cms\model\data\CelebrityImage::IMAGE_TYPE_OTHER]->ImageId],'/'));
	    	}else{
	    		array_push($arrValues,null);
	    	}
    	}
    	
    	$arrSql = array_pad($arrSql,count($arrValues),'%s');
    	$sql = 'insert into `library_celebrity` ('.$field.') values ';
    	$sql .=  '(' . implode(',',$arrSql) . ')';
    	$this->setConfig('haibao_article');
    	$this->query($sql, $arrValues);
    	
    	$oldTableCelebrityId = $this->getDb()->insert_id;
    	
    	// 添加明星和品牌的关联关系
    	if(!empty($model->CelebrityRelBrand)){
    		$sqlArr = $valArr = array();
    		$sql = 'insert into `brand_rel_star` (new_star_id,brand_id,date) values ';
    		foreach($model->CelebrityRelBrand as $key=>$arr){
    			array_push($sqlArr,'(%s,%s,%s)');
    			array_push($valArr,$oldTableCelebrityId,$arr['id'],$arr['time']);
    		}
    		$sql .= implode(',',$sqlArr);
    		$this->query($sql,$valArr);
    		/*
    		$brandIdArr = array();
    		foreach($model->CelebrityRelBrand as $key=>$arr){
    		
    			array_push($brandIdArr,$arr['id']);
    		}
    		
    		// DOTO : 获取品牌的信息
    		$brandData = new \haibao\cms\data\Brand();
    		$brands = $brandData->getBrandById($brandIdArr);

    		$sqlArr = $valArr = array();
    		if($brands){
				
    			$sql = 'insert into `library_brand_star_extend` (brandid,starid,tag_desc,tag_story) values ';
    			foreach($brands as $brand){
    				array_push($sqlArr,'(%s,%s,%s,%s)');
    				array_push($valArr,$brand->Id,$model->Id,$brand->Description,$brand->Story);
    			}
    		}
    		$sql .= implode(',',$sqlArr);
    		$this->query($sql,$valArr);
    		*/
    	}
    	
    	// 添加明星与明星的关联关系
    	if(!empty($model->CelebrityRelStar)){
    		 
    		$topicId = \haibao\cms\business\Tag::getTopicIdByTagId(array($model->TagId));
    	
    		foreach($model->CelebrityRelStar as $key=>$arr){

    			$this->setConfig(\haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_ARTICLE);
    			$sqlArr = $valArr = array();
    			$sql = 'insert into `star_rel` (star_1,star_2,type,status) values (%s,%s,%s,%s)';
    			array_push($valArr,$topicId[$model->TagId],$arr['id'],$arr['type'],1);
    			$this->query($sql,$valArr);
    			$oldId = $this->getDb()->insert_id;
    			$this->setConfig(\haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_CMS);
    			$sql = "insert into DataMigrationMap (TableName,PrimaryKey,OldPrimaryKey) ";
    			$sql .= "values('CelebrityRelation',".$arr['newId'].",".$oldId.")";
    			$this->query($sql);
    		}
    	}
    		
    	$celebrityOldId = $this->getDb()->insert_id;
    
    	//添加新老数据对应表DataMigrationMap
    	$this->setConfig(\haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_CMS);
    	$sql = "insert into DataMigrationMap (TableName,PrimaryKey,OldPrimaryKey) ";
    	$sql .= "values('".$model->getTableName()."',".$model->Id.",".$celebrityOldId.")";
    	$this->query($sql);
    }
    
    /**
     * 修改旧库明星
     * @param Object \haibao\cms\model\data\Celebrity $celebrityModel
     */
    private function updateLibraryCelebrity($model){
        $sql = 'update `library_celebrity` set `desc`=%s,`name_ch`=%s,`name_en`=%s,`gender`=%s,`place`=%s,`startypes`=%s,`birthday`=%s,`pinyin_ch`=%s,`pinyin_en`=%s,`status`=%s,`date`=%s,`more_ch`=%s,`more_en`=%s,`die_date`=%s,`xingzuo`=%s,`birth_place`=%s,`height`=%s,`blood`=%s,`school`=%s,`ec_company`=%s,`rp_works`=%s,`success`=%s,`family`=%s ,`update`=%s ,`avatar`=%s,`avatar_640`=%s,`avatar_710`=%s where tag_id=%s';
    	$arrValues = array();
    	array_push($arrValues, $model->Description);
    	array_push($arrValues, $model->NameCn);
    	array_push($arrValues, $model->NameEn);
    	array_push($arrValues, $model->Gender);
    	array_push($arrValues, $model->Place);
    
    	array_push($arrValues, $model->StarTypes);
    	array_push($arrValues, $model->Birthday);
    	array_push($arrValues, $model->PinyinCh);
    	array_push($arrValues, $model->PinyinEn);
    	if($model->Status == 0){
    		$status = 2;
    	}else{
    		$status = 1;
    	}
    	array_push($arrValues, $status);
    	array_push($arrValues, date('Y-m-d H:i:s'));
    	array_push($arrValues, $model->CelebrityInfo->MoreChName);
    	array_push($arrValues, $model->CelebrityInfo->MoreEnName);
    	array_push($arrValues, $model->CelebrityInfo->DieDate);
    	array_push($arrValues, $model->CelebrityInfo->XingZuo);
    	array_push($arrValues, $model->CelebrityInfo->BirthPlace);
    	array_push($arrValues, $model->CelebrityInfo->Height);
    	array_push($arrValues, $model->CelebrityInfo->Blood);
    	array_push($arrValues, $model->CelebrityInfo->School);
    	array_push($arrValues, $model->CelebrityInfo->EcCompany);
    	array_push($arrValues, strip_tags($model->CelebrityInfo->RepWorks));
    	array_push($arrValues, strip_tags($model->CelebrityInfo->Success));
    	array_push($arrValues, strip_tags($model->CelebrityInfo->Family));
    	array_push($arrValues, date('Y-m-d H:i:s'));
    	
    	// 处理图片
    	$imageIds = array();
    	foreach($model->CelebrityImage as $image){
    		$imageIds[$image->ImageType] = $image->ImageId;
    	}
    	$images = \haibao\cms\business\ImageLibrary::getImagesById($imageIds);
    	$imgArr = array();
    	foreach($images as $img){
    		$imgArr[$img->Id] = $img->Filename;
    	}
    	if($model->CelebrityImage[\haibao\cms\model\data\CelebrityImage::IMAGE_TYPE_PICTURE]->ImageId){
    		array_push($arrValues, $imgArr[$model->CelebrityImage[\haibao\cms\model\data\CelebrityImage::IMAGE_TYPE_PICTURE]->ImageId]);
    	}else{
    		array_push($arrValues,"");
    	}
    	if($model->CelebrityImage[\haibao\cms\model\data\CelebrityImage::IMAGE_TYPE_SCREEN]->ImageId){
    		array_push($arrValues, $imgArr[$model->CelebrityImage[\haibao\cms\model\data\CelebrityImage::IMAGE_TYPE_SCREEN]->ImageId]);
    	}else{
    		array_push($arrValues,"");
    	}
    	if(array_key_exists(\haibao\cms\model\data\CelebrityImage::IMAGE_TYPE_OTHER, $model->CelebrityImage)){
    		if($model->CelebrityImage[\haibao\cms\model\data\CelebrityImage::IMAGE_TYPE_OTHER]->ImageId){
    			array_push($arrValues, $imgArr[$model->CelebrityImage[\haibao\cms\model\data\CelebrityImage::IMAGE_TYPE_OTHER]->ImageId]);
    		}else{
    			array_push($arrValues,"");
    		}
    	}else{
    		array_push($arrValues,null);
    	}
    	
    	array_push($arrValues, $this->getOneById($model->Id)->TagId);
    	$this->setConfig(\haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_ARTICLE);
    	$this->query($sql, $arrValues);

    	// 修改老表的明星与品牌的关联关系
    	$sql = 'select id from `library_celebrity` where tag_id=%s limit 1';
    	$result = $this->query($sql,array($model->TagId));
    	while($result->fetch_assoc()){
    		 
    		$oldTableCelebrityId = $result->getData('id');
        }
        if(isset($oldTableCelebrityId)){
            $sql = 'delete from `brand_rel_star` where new_star_id=%s';
            $this->query($sql,array($oldTableCelebrityId));
            if(!empty($model->CelebrityRelBrand)){

                $sqlArr = $valArr = array();
                $sql = 'insert into `brand_rel_star` (new_star_id,brand_id,date) values ';
                foreach($model->CelebrityRelBrand as $key=>$arr){
                    array_push($sqlArr,'(%s,%s,%s)');
                    array_push($valArr,$oldTableCelebrityId,$arr['id'],$arr['time']);
                }
                $sql .= implode(',',$sqlArr);
                $this->query($sql,$valArr);
            /*
            $brandIdArr = array();
            foreach($model->CelebrityRelBrand as $key=>$arr){

                array_push($brandIdArr,$arr['id']);
            }

            // 获取品牌的信息
            $brandData = new \haibao\cms\data\Brand();
            $brands = $brandData->getBrandById($brandIdArr);

            $sqlArr = $valArr = array();
            if($brands){
                $sql = 'insert into `library_brand_star_extend` (brandid,starid,tag_desc,tag_story) values ';
                foreach($brands as $brand){
                    array_push($sqlArr,'(%s,%s,%s,%s)');
                    array_push($valArr,$brand->Id,$model->Id,$brand->Description,$brand->Story);
                }
            }
            $sql .= implode(',',$sqlArr);
            $this->query($sql,$valArr);
             */
            }
        }
        // 修改明星与明星的关联关系
        $topicId = \haibao\cms\business\Tag::getTopicIdByTagId(array($model->TagId));
        $sql = 'delete from `star_rel` where star_1 = %s';
        $this->query($sql,array($topicId[$model->TagId]));
        if(!empty($model->CelebrityRelStar)){

            $sql = 'insert into `star_rel` (star_1,star_2,type,status) values ';

            $sqlArr = $valArr = array();
            foreach($model->CelebrityRelStar as $key=>$arr){
                
                $this->setConfig(\haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_ARTICLE);
                $sqlArr = $valArr = array();
                $sql = 'insert into `star_rel` (star_1,star_2,type,status) values (%s,%s,%s,%s)';
                array_push($valArr,$topicId[$model->TagId],$arr['id'],$arr['type'],1);
                $this->query($sql,$valArr);
                $oldId = $this->getDb()->insert_id;
                $this->setConfig(\haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_CMS);
                $sql = "insert into DataMigrationMap (TableName,PrimaryKey,OldPrimaryKey) ";
                $sql .= "values('CelebrityRelation',".$arr['newId'].",".$oldId.")";
                $this->query($sql);
            }
        }

    }
}
